package com.uwntek.service.Imp;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.base.service.impl.ServiceImpl;
import com.uwntek.base.des.DesSecurity;
import com.uwntek.pojo.MySessionFactory;
import com.uwntek.pojo.Projectinfo;
import com.uwntek.service.IProjectService;


public class ProjectServiceImpl<T extends Projectinfo> extends ServiceImpl<T> implements
		IProjectService<T> {

	@Override
	public boolean create(T Projectinfo) {

		if (dao.createQuery(
				" from Projectinfo d where d.projectId = :name ")
				.setParameter("name", Projectinfo.getProjectId()).list().size() > 0) {
			return false;
		}else{
		dao.create(Projectinfo);
		return true;}

	}
	public List<Projectinfo> getProjectList(String projectId,String projectName) throws Exception
	{
		List<Projectinfo> list = null;
		int count = 0;
		StringBuffer sql = new StringBuffer("from Projectinfo p ");
		DesSecurity des = new DesSecurity("UWNTEK_CLIENT", "12345678");
		if(projectId!=null&&!projectId.equals("")||(projectName!=null&&!projectName.equals(""))){
			sql.append("where");
			if(projectId!=null&&!projectId.equals("")){
				sql.append(" p.projectId like '" +des.encrypt(projectId)+"'");
				count+=1;
			}
			if(null!=projectName&&!projectName.equals("")){
				if(count !=0)
					{sql.append(" and p.projectName like '"+des.encrypt(projectName)+"'");
				//System.out.println(des.encrypt(projectName));
					}
				else
					{sql.append(" p.projectName like '"+des.encrypt(projectName)+"'");
				//	System.out.println(des.encrypt(projectName));
					}
			}
		}
		String hql = sql.toString();
		list = dao.createQuery(hql).list();
		return list;
	}
	public List<Projectinfo> getDesList(List<Projectinfo> projectList,Projectinfo project) throws Exception{
		DesSecurity des = new DesSecurity("UWNTEK_CLIENT", "12345678");
		List<Projectinfo> list=new ArrayList<Projectinfo>();
		for(int i=0;i<projectList.size();i++){
			project = projectList.get(i);
			project.setProjectId(des.decryptStrUTF(project.getProjectId()));
			project.setProjectName(des.decryptStrUTF(project.getProjectName()));
			project.setUrl(des.decryptStrUTF(project.getUrl()));
			project.setComment(des.decryptStrUTF(project.getComment()));
			list.add(project);
		}
		return list;
		
	}
	
	public boolean saveProjectInfo(Projectinfo project) throws Exception {
		Session session = MySessionFactory.getSession();
		Transaction tx = null;
		boolean flag = false;
		try {
			tx = session.beginTransaction();
			session.save(project);
			tx.commit();
			flag = true;
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			MySessionFactory.closeSession();
		}
		return flag;
	}
	
	public List queryProjectInfo(String sql) throws Exception {
		Session session = MySessionFactory.getSession();
		Transaction tx = null;
		List list =null;
		try {
			Query query = session.createQuery(sql);
			tx = session.beginTransaction();
			list = query.list();
			tx.commit();
			if (!Hibernate.isInitialized(list))
				Hibernate.initialize(list);
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			MySessionFactory.closeSession();
		}
		return list;
	}

	public boolean updateProjectInfo(Projectinfo project) throws Exception {
		Session session = MySessionFactory.getSession();
		Transaction tx = null;
		boolean flag = false;
		try {
			tx = session.beginTransaction();
			session.update(project);
			tx.commit();
			flag = true;
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			MySessionFactory.closeSession();
		}
		return flag;
	}
}
